package com.surfilter.msmp.common.util;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述：HTML页面解析相关方法
 * 作者：JinHuaTao
 * 时间：2018/6/20 14:03
 */
public class HtmlUtil {

    private static final Logger LOG = LoggerFactory.getLogger(HtmlUtil.class);

    private static Pattern patt = Pattern.compile("<title[^<^>]*>([^<^>]*)</title>");

    /**
     * 获取HTML文本中的title
     * */
    public static String getHtmlTitle(String html){
        String result = "";
        try{
            if(html == null){
                return result;
            }
            Matcher match = patt.matcher(html);
            String retitle = null;
            while(match.find()){
                retitle = match.group();
                if(retitle.length() > 0){
                    break;
                }
            }
            if(retitle == null){
                return result;
            }
            retitle = retitle.replace("\n", "").replace("\t", "").trim();
            result = retitle.replaceAll("</?title[^<^>]*>", "");
        }catch(Exception e){
            LOG.error("获取HTML Title异常", e);
        }
        return result;
    }

    /**
     * 获取Html文本中的<meta>描述词
     * */
    public static String getHtmlDescription(String html){
        String result = null;
        try{
            String regex1 = "<meta\\s*(http-equiv|name)\\s*=\\s*\"(\\s*[kK][eE][yY][wW][oO][rR][dD][sS]|description|[kK][eE][yY][wW][oO][rR][dD])\\s*\"\\s*content\\s*=\\s*\"[^<^>]*\"\\s*>(([^<^>]*)</meta>)?";
            String regex2 = "content\\s*=\\s*\"[^<^>]*\"";
            String regex3 = "\".*\"";
            result = parseString(html, regex1, true);
            result =parseString(result, regex2, true);
            result = parseString(result, regex3, true);
            result = result.replaceAll("\"", "");
        }catch(Exception e){
            LOG.error("匹配Html Description异常", e);
        }
        return result;
    }

    /**
     * Html页面词频统计
     */
    public static Map<String, Integer> getHtmlWordFrequencyCount(String text, int minLength, int maxLength){
        Map<String, Integer> result = new HashMap<String, Integer>();
        //匹配中文正则
        String regex = "[\u4E00-\u9FA5]*";
        text = parseString(text, regex, true);
        if(text != null && !text.trim().equalsIgnoreCase("")){
            text = text.trim();
            if(text.length() >= minLength){
                for(int i = 0; i < text.length() - minLength; i ++){
                    if(text.length() - i >= minLength){
                        for(int j = minLength ; j <= ((text.length() - i) > maxLength ? maxLength : (text.length() - i)) ; j++){
                            String str = text.substring(i, i + j);
                            if(result.containsKey(str)){
                                result.put(str, result.get(str) + 1);
                            }else{
                                result.put(str, 1);
                            }
                        }
                    }else{
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 获取Html文本中的Body文本
     * */
    public static List<String> getHtmlBodyText(String html){
        List<String> result = new ArrayList<String>();
        try{
            Document doc = Jsoup.parse(html);
            String bodyText = "";
            if(doc != null){
                Element element = doc.body();
                String title = doc.title();
                if(element != null){
                    bodyText = doc.body().text();
                }else {
                    LOG.debug(">>>>>>>>>>>>>>>>>HTML的body为空》》》》[" + title + "]");
                }
            }
            result.addAll(Arrays.asList(bodyText.split(" ")));
        }catch(Exception e){
            LOG.error("匹配Body Texts Three异常" + e);
        }
        return result;
    }

    /**
     * 解析字符串正则
     * */
    public static String parseString(String text, String regex, boolean regexAll){
        StringBuffer strb = new StringBuffer();
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        while(match.find()){
            strb.append(match.group());
            if(!regexAll){
                break;
            }
        }
        return strb.toString();
    }

    /**
     * @Description: 根据domain解析本地快照存放路径，并根据路径加载html文件
     * @param domain
     * @return String
     */
    public static String getHtmlFromDomain(String domain){
        String html = "";
        String snapshotFilePath = FilePathUtil.getFilePathByDomainPartion(domain);
        File file = new File(snapshotFilePath);
        if(file.exists()){
            html = getHtmlFromFile(snapshotFilePath);
        }else{
            LOG.warn( domain + "快照文件不存在!");
        }
        return html;
    }

    /**
     * 从本地文件中读取HTML
     * */
    public static String getHtmlFromFile(String filePath){
        String html = "";
        File file = new File(filePath);
        if (file.exists()) {
            try {
                html = FileUtils.readFileToString(file, "UTF-8");
            } catch (IOException e) {
                LOG.error("读取" + filePath +"文件失败:");
            }
        }else{
            LOG.warn(">>>>>>>>文件不存在:" + filePath);
        }
        return html;
    }


}
